from drf_spectacular.utils import extend_schema, inline_serializer
from rest_framework import status as ResponseStatus
from rest_framework.decorators import action
from rest_framework.mixins import (CreateModelMixin, ListModelMixin,
                                   RetrieveModelMixin, UpdateModelMixin)
from rest_framework.viewsets import GenericViewSet
from backend.cews.services.LogService import get_log_by_log_type, filter_log, get_depot_log_by_part, get_depot_log_by_car
from utils.autoschema import AutoDocSchema, custom_response_serailizer
from utils.decorators import request_data

from backend.cews.models import Log, RepairOrder

from .serializers import (LogSerializer)
from rest_framework.response import Response
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 100
    page_size_query_param = 'page_size'
    page_query_param='page'
    max_page_size = 100

class LogViewSet(
    RetrieveModelMixin,
    ListModelMixin,
    UpdateModelMixin,
    GenericViewSet,
    CreateModelMixin
):
    serializer_class = LogSerializer
    queryset = Log.objects.all()
    pagination_class = StandardResultsSetPagination

    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    @request_data(keys=['log_type','repair_order_id'])
    def get_log_by_type(self, request, log_type, repair_order_id):
        print('data',log_type,repair_order_id)
        logs = get_log_by_log_type(log_type, repair_order_id)
        
        data = LogSerializer(logs, many=True).data
        return Response(status=200,
                        data={
                            'code': 1,
                            'data': data
                        })
 
    @action(detail=False, methods=['POST'], schema=AutoDocSchema())
    @request_data(keys=['log_type','action_type','category_type','stock_code','serial_no','job_order','dateFrom','dateTo','currentPage'],
                  optional=['log_type','action_type','category_type','stock_code','serial_no','job_order','dateFrom','dateTo','currentPage'])
    def filter_log(self, request, log_type, action_type, category_type, stock_code, serial_no,job_order, dateFrom, dateTo , currentPage):
        logs = filter_log(log_type, action_type, category_type, stock_code, serial_no,job_order, dateFrom, dateTo, currentPage)
        
        # data= LogSerializer(logs, many=True).data
        # return Response(status=200,
        #                 data={
        #                     'code': 1,
        #                     'data': {
        #                         "logs": data,
        #                         "max" : max
        #                     }
        #                 })
        paginator = StandardResultsSetPagination()
        page = paginator.paginate_queryset(request=request,queryset=logs)

        # if page is not None:
        serializer = LogSerializer(page, many=True)
        return paginator.get_paginated_response(serializer.data)
        
        # data= LogSerializer(logs, many=True).data
        # return Response(status=200,
        #                 data={
        #                     'code': 1,
        #                     'data': {
        #                         "logs": data,
        #                         "max" : max
        #                     }
        #                 })
        # serializer = self.get_serializer(queryset, many=True)
        # return Response(serializer.data)

    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    def get_order_opts(self, request):
        
        ret = {}
        order = RepairOrder.objects.filter(deleted=False)
        sc_log = Log.objects.order_by('stock_code').filter(deleted=False).distinct('stock_code')
        sn_log = Log.objects.order_by('part').filter(deleted=False).exclude(part=0).exclude(part='').distinct('part')

        ret['jo'] = [o.order_no for o in order]
        ret['stock_codes'] = [l.stock_code for l in sc_log if len(l.stock_code)>8]
        ret['serial_nos'] = [l.part for l in sn_log]
        ret['log_types'] = [c[0] for c in Log.log_type.field.choices]
        ret['action_types'] = [c[0] for c in Log.action_type.field.choices]
        ret['category_types'] = [c[0] for c in Log.category_type.field.choices]

        
        return Response(data=ret, status=200)
    
    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    @request_data(keys=['stock_code'])
    def get_serial_no_by_stock_code(self, request, stock_code):
        
        ret = {}
        sn_log = Log.objects.order_by('part').filter(stock_code=stock_code,deleted=False).exclude(part=0).exclude(part='').distinct('part')
        print(sn_log)
        ret['serial_nos'] = [l.part for l in sn_log]

        
        return Response(data=ret, status=200)

    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    @request_data(keys=['stock_code', 'serial_no'])
    def get_depot_log_by_part(self, request, stock_code, serial_no):
        
        logs =  get_depot_log_by_part(stock_code, serial_no)
        
        return Response(data=LogSerializer(logs, many=True).data, status=200)
    
    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    @request_data(keys=['car'])
    def get_depot_log_by_car(self, request, car):
        
        logs =  get_depot_log_by_car(car)
        
        return Response(data=LogSerializer(logs, many=True).data, status=200)